#include "LoginWidget.h"
#include "ui_LoginWidget.h"
#include "smartphone/Smartphone.h"
#include <QDebug>
#include <unistd.h>
LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    connect(Smartphone::getInstance(), SIGNAL(signalRegistrationStateChanged(SmartphoneState,QString,QString)),
            this, SLOT(slotRegistrationStateChanged(SmartphoneState,QString,QString)));
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_btnLogin_clicked()
{

    Smartphone::getInstance()->login(ui->edtServer->text(), (SmartphoneTransportType)0, ui->edtDispname->text(),
                                     ui->edtUsername->text(), ui->edtPasswd->text(), ui->edtExpires->text().toInt());

}

void LoginWidget::on_btnLogout_clicked()
{
    Smartphone::getInstance()->logout();
}

void LoginWidget::slotRegistrationStateChanged(SmartphoneState state, QString proxy, QString message)
{
    switch(state)
    {
    case SmartphoneRegistrationNone:

        ui->labelStatus->setText("未登录!");
        break;
    case SmartphoneRegistrationProgress:
        ui->labelStatus->setText("正在登录...");
        break;
    case SmartphoneRegistrationOk:
        ui->labelStatus->setText("登录成功!");
        break;
    case SmartphoneRegistrationCleared:
        ui->labelStatus->setText("已注销!");
        break;
    case SmartphoneRegistrationFailed:
        ui->labelStatus->setText("登录失败!");
        break;
    default:
        break;
    }
    ui->labelMessage->setText(message);
}

void LoginWidget::on_pushButton_clicked()
{
    Smartphone::getInstance()->refreshRegisters();
}
